package huoye.shops.utils;

import android.app.Activity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.codeideology.android.lang.entity.Paging;
import com.codeideology.android.widget.recycler.RecyclerViewAdapter;
import com.codeideology.android.widget.swiperefresh.AXIRefreshLayout;
import com.codeideology.android.widget.swiperefresh.layout.api.RefreshLayout;
import com.codeideology.android.widget.swiperefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.List;

/**
 * Created by user on 2016/11/17.
 */

public class PullToReshHelper<A extends RecyclerViewAdapter, E> implements OnRefreshLoadmoreListener {

    private AXIRefreshLayout mRefreshLayout;
    private RecyclerView mRecycler;
    protected View emptyView;

    public A getAdapter() {
        return mAdapter;
    }

    public void setAdapter(A mAdapter) {
        this.mAdapter = mAdapter;
    }

    private A mAdapter;


    public void setQueryMore(boolean queryMore) {
        this.queryMore = queryMore;
    }

    private boolean queryMore;

    private OnRefreshListener onRefreshListener;
    private Activity activity;

    public void setLayoutManager(RecyclerView.LayoutManager layoutManager) {
        this.layoutManager = layoutManager;
    }

    private RecyclerView.LayoutManager layoutManager;

    public void setEmptyView(View emptyView) {
        this.emptyView = emptyView;
    }

    boolean isAlter = false;

    public PullToReshHelper(final Activity activity, AXIRefreshLayout refreshLayout, RecyclerView recycler, OnRefreshListener onRefreshListener, boolean isAlterManager) {
        this.activity = activity;

        this.mRefreshLayout = refreshLayout;
        this.mRefreshLayout.setOnRefreshLoadmoreListener(this);

        this.mRecycler = recycler;
        this.onRefreshListener = onRefreshListener;
        this.isAlter = isAlterManager;

    }

    public void init() {

        if (mAdapter != null) {
            if (layoutManager == null) {
                if(isAlter){
                    layoutManager = new GridLayoutManager(activity,2);
                }else {
                    layoutManager = new LinearLayoutManager(activity);
                }


            }
            mRecycler.setLayoutManager(layoutManager);
            mRecycler.setAdapter(mAdapter);
        }

    }

    public void validateEmpty() {

        if (emptyView != null) {
            if (mAdapter != null && mAdapter.getListCount() > 0) {
                emptyView.setVisibility(View.GONE);
            } else {
                emptyView.setVisibility(View.VISIBLE);
            }
        }


        endRefreshing();
    }

    public void endRefreshing() {
        if (mRefreshLayout != null) {
            mRefreshLayout.finishRefresh();
            mRefreshLayout.finishLoadmore();
        }
    }

    public Paging getPaging() {
        return paging;
    }

    private Paging paging = new Paging();

    public void setDataset(final List<E> data, final Paging paging) {

        if (!queryMore) {
            mAdapter.clear();
        }

        endRefreshing();

        if (!queryMore) {
            mAdapter.clear();
        }

        mAdapter.addMoreData(data);
        if (!queryMore) {
            mRecycler.smoothScrollToPosition(0);
        }
        if (paging != null) {
            PullToReshHelper.this.paging = paging;
        }
        validateEmpty();

    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        onRefreshListener.processQuery(new Paging());
        queryMore = false;
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        if (!mRefreshLayout.isLoadmoreFinished()) {
            if (onRefreshListener.isQueryMore()) {
                int page = 1;
                if (paging != null && mAdapter != null) {
                    page = mAdapter.getListCount() % paging.getPageSize() == 0 ? mAdapter.getItemCount() / paging.getPageSize() + 1 : mAdapter.getItemCount() / paging.getPageSize() + 2;
                }
                paging.setCurrentPage(page);
                onRefreshListener.processQuery(paging);
                queryMore = true;
            }else {
                mRefreshLayout.finishLoadmore();
            }
        }
    }

    public interface OnRefreshListener {
        void processQuery(Paging paging);

        boolean isQueryMore();
    }
}

